package org.aeon.newsmanagersystem.service.impl;

import org.aeon.newsmanagersystem.dao.NewsMapper;
import org.aeon.newsmanagersystem.dao.TopicMapper;
import org.aeon.newsmanagersystem.pojo.Topic;
import org.aeon.newsmanagersystem.service.TopicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: MissingAeon
 * @date: 2024/3/27 17:30
 * @description:
 */
@Service
public class TopicServiceImpl implements TopicService {
    private final TopicMapper topicMapper;
    private final NewsMapper newsMapper;

    @Autowired
    public TopicServiceImpl(TopicMapper topicMapper, NewsMapper newsMapper) {
        this.topicMapper = topicMapper;
        this.newsMapper = newsMapper;
    }

    @Override
    public List<Topic> display() {
        return topicMapper.all();
    }

    @Override
    public int add(Topic topic) {
        if (exists(topic)) {
            return 0;
        }
        return topicMapper.add(topic);
    }

    @Override
    public int modify(Topic topic) {
        if (exists(topic)) {
            return 0;
        }
        return topicMapper.updateByPrimaryKey(topic);
    }

    @Override
    public boolean exists(Topic topic) {
        if (topic.getTid() != null) {
            return topicMapper.countExclusion(topic.getTname(), topic) > 0;
        }
        return topicMapper.count(topic.getTname()) > 0;
    }

    @Override
    public Topic get(Integer id) {
        return topicMapper.selectByPrimaryKey(id);
    }

    @Override
    public int delete(Integer topicId) {
        return topicMapper.deleteByPrimaryKey(topicId);
    }
}
